/**
 * created by 江心才子, 2019/8/31
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.libbase.base

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableInt
import androidx.databinding.ViewDataBinding
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.wyl.libbase.extend.logD

/**
 * 项目名称：O2o
 * 创建人：江心才子
 * 创建时间：2019-08-31 15:47
 * 内容描述：
 * 修改说明：
 */
abstract class PageFragment<VB : ViewDataBinding, T> : BindingFragment<VB>() {
    protected var page = 1
    protected val pageSize = 10
    protected var totalSize = page * pageSize

    val dataSource = ObservableArrayList<T>()

    val loadListener by lazy {
        object : RefreshListenerAdapter() {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout) {
                refresh.set(true)
                refresh()
            }

            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout) {
                loadMore.set(true)
                loadMore()
            }

            override fun onRefreshCanceled() {
                logD("onRefreshCanceled")
            }

            override fun onFinishRefresh() {
                logD("onFinishRefresh")
            }
        }

    }

    open val enableRefresh = ObservableBoolean(true)

    open val enableLoadMore = ObservableBoolean(true)

    /**
     * 是否正在刷新
     */
    val refresh = ObservableBoolean(false)

    /**
     * 是否正在加载更多
     */
    val loadMore = ObservableBoolean(false)

    /**
     * 是否展示空页面
     */
    val empty = ObservableInt(View.GONE)

    abstract fun refresh()

    abstract fun loadMore()
}